package ${packageStr};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;

import java.util.Collections;
import java.util.List;

import com.ruoyi.domain.OrderEntity;
import ${entityPath};
import ${mapperPath};
import ${servicePath};

@Slf4j
@Service
/**${entity.tableComment} Service实现类*/
public class ${serviceImplName} extends ServiceImpl<${mapperName},${entityName}> implements ${serviceName}{
    @Autowired
    private ${mapperName}   ${repostioryName};

    /**分页查询*/
    @Override
    public IPage<${entityName}> pageList(Page<${entityName}> page, ${entityName} entity, OrderEntity orderEntity) {
        LambdaQueryWrapper<${entityName}> countWrapper = new LambdaQueryWrapper<>(entity);
        setQueryTime(countWrapper,entity);
        countWrapper.select(${entityName}::${idMethod});
        int count = count(countWrapper);

        List<${entityName}> lists = Collections.emptyList();
        if (count > 0) {
            PageHelper.startPage((int)page.getCurrent(),(int)page.getSize(),false);
            LambdaQueryWrapper<${entityName}> selectWrapper = new LambdaQueryWrapper<>(entity);
            setQueryTime(selectWrapper,entity);
            orderColumn(selectWrapper,orderEntity);
            lists = list(selectWrapper);
       }

        IPage<${entityName}> returnPage = new Page<${entityName}>();
        returnPage.setRecords(lists);
        returnPage.setPages(count % page.getSize() == 0 ? count / page.getSize() : count / page.getSize() + 1);
        returnPage.setCurrent(page.getCurrent());
        returnPage.setSize(page.getSize());
        returnPage.setTotal(count);

        return returnPage;
    }

    @Override
    public List<${entityName}> list(${entityName} entity) {
        LambdaQueryWrapper<${entityName}> selectWrapper = new LambdaQueryWrapper<>(entity);
        setQueryTime(selectWrapper,entity);
        return list(selectWrapper);
    }
   // @Override
   // public List<${entityName}> listOfSelect(${entityName} entity) {
  //      LambdaQueryWrapper<${entityName}> wrapper = new LambdaQueryWrapper<>(entity);
   //     wrapper.select(${entityName}::, ${entityName}::);
  //      return list(wrapper);
  //  }

   // @Override
   // public List<${entityName}> listOfIds(java.util.Collection<${idType}> ids) {
   //     if (org.springframework.util.CollectionUtils.isEmpty(ids)) {
    //         return java.util.Collections.emptyList();
    //    }
     //   LambdaQueryWrapper<${entityName}> wrapper = new LambdaQueryWrapper<>();
     //   wrapper.select(${entityName}::${idMethod},${entityName}::);
     //   wrapper.in(${entityName}::${idMethod}, ids);
     //   return list(wrapper);
   // }

    @Override
    public ${entityName} getOne(${entityName} entity) {
        LambdaQueryWrapper<${entityName}> selectWrapper = new LambdaQueryWrapper<>(entity);
        setQueryTime(selectWrapper,entity);
        return getOne(selectWrapper);
    }

    @Override
    public Integer countId(${entityName} entity) {
    LambdaQueryWrapper<${entityName}> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.select(${entityName}::${idMethod});
        setQueryTime(wrapper,entity);
        return count(wrapper);
    }


    /**插入有值的列  */
    @Override
    public int insertSelective(${entityName} entity){
       return ${repostioryName}.insertSelective(entity);
    }

    /**插入数据  */
    @Override
    public boolean insert(${entityName} entity){
       return save(entity);
    }

    /**根据主键修改数据*/
    @Override
    public boolean  updateByPrimaryKey(${entityName} entity){
       return updateById(entity);
    }

    <#if "!statucFlag" !="1">
        /***根据主键删除数据*/
        @Override
       public boolean deleteById(${idType} ${idFiled}){
            return removeById(${idFiled});
       }
    </#if>


    public static void orderColumn(LambdaQueryWrapper<${entityName}> wrapper,com.ruoyi.domain.OrderEntity orderEntity){
        if(org.apache.commons.lang3.StringUtils.equals("ascending",orderEntity.getOrder())){
             <#list orderMethodList  as  method>
                 <#if "${method.columnFlag}"=="1">
                 if(org.apache.commons.lang3.StringUtils.equals(orderEntity.getProp(),"${method.filed}")){
                    wrapper.orderByAsc(${entityName}::${method.getName});
                 }
                 </#if>
             </#list>
        }else if(org.apache.commons.lang3.StringUtils.equals("descending",orderEntity.getOrder())){
            <#list orderMethodList  as  method>
                <#if "${method.columnFlag}"=="1">
                if(org.apache.commons.lang3.StringUtils.equals(orderEntity.getProp(),"${method.filed}")){
                    wrapper.orderByDesc(${entityName}::${method.getName});
                }
                </#if>
            </#list>
        }else{
            wrapper.orderByDesc(${entityName}::${idMethod});
        }
    }

    public static com.ruoyi.domain.OrderEntity orderColumn(com.ruoyi.domain.OrderEntity orderEntity){
        if(org.apache.commons.lang3.StringUtils.equals("ascending",orderEntity.getOrder())){
        <#list orderMethodList  as  method>
            <#if "${method.columnFlag}"=="1">
            if(org.apache.commons.lang3.StringUtils.equals(orderEntity.getProp(),"${method.filed}")){
                 return new com.ruoyi.domain.OrderEntity("asc","${method.dataColumn}");
            }
            </#if>
        </#list>
        }else if(org.apache.commons.lang3.StringUtils.equals("descending",orderEntity.getOrder())){
        <#list orderMethodList  as  method>
             <#if "${method.columnFlag}"=="1">
            if(org.apache.commons.lang3.StringUtils.equals(orderEntity.getProp(),"${method.filed}")){
                return new com.ruoyi.domain.OrderEntity("desc","${method.dataColumn}");
            }
             </#if>
        </#list>
        }
        return new com.ruoyi.domain.OrderEntity("desc","${idColumn}");
    }

    public void setQueryTime(LambdaQueryWrapper<${entityName}> wrapper,${entityName} entity){
        <#list orderMethodList  as  method>
            <#if "${method.startFiledNameValue}"=="1">
                if(java.util.Objects.nonNull(entity.${method.startFiledName}()) && java.util.Objects.nonNull(entity.${method.getName}())){
                    wrapper.between(${entityName}::${method.getFileName},entity.${method.startFiledName}(),entity.${method.getName}());
                }else if(java.util.Objects.nonNull(entity.${method.startFiledName}())){
                wrapper.ge(${entityName}::${method.getFileName},entity.${method.startFiledName}());
                }else if(java.util.Objects.nonNull(entity.${method.getName}())){
                wrapper.le(${entityName}::${method.getFileName},entity.${method.getName}());
                }
            </#if>
        </#list>
    }

}